package com.hliushi.netty.c3;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author llhuang10
 * @date 2021/10/19 20:14
 */
@Slf4j
public class TaskJdkFuture {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 1.线程池
        ExecutorService service = Executors.newFixedThreadPool(2);
        // 2.提交任务
        Future<Integer> future = service.submit(() -> {
            log.debug("执行计算");
            try {
                Thread.sleep(1000);
                return 50;
            } catch (InterruptedException e) {
                e.printStackTrace();
                return -1;
            }
        });

        // 3.主线程通过future来获取结果
        log.debug("main线程阻塞等待Future线程的结果");
        log.debug("Future线程的结果是 {}", future.get());


        service.shutdown();
    }
}
